package com.makinolo.rlvforge.tacx.file
{
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.utils.ByteArray;
import flash.utils.Endian;

import com.makinolo.rlvforge.tacx.data.*;

public class RLVDefinition
{
	
	// A RLV definition file consists of a Header, a RLV Info block, frame/distance mappings
	// infoboxes and course info  
	private var m_head:Header;
	private var m_info:RLVInfo;
	private var m_fdmapping:Array; 			//This block contains a number of records which appear to indicate 
											 //the average speed of various parts of the video were filmed at.
	private var m_infobox:Array;
	private var m_courseinfo:Array; // There can be more than one of these - one for each route segment.
	
	private var m_infoboxtext:String;	// Text to be written to commands.txt with infobox contents
/*	public function read(InputStream is:)
	{		
		try
		{												
			ByteArray dis=new ByteArray(is);
			
			m_head = new Header(dis);
			if (m_head.getFileFingerprint() != Header.RLV_FINGERPRINT)
			{
				throw new Exception("Wrong Tacx file type "+m_head.getFileFingerprint());
			}
			else
			{
				InfoBlock iblock;
				for (int i=0; i<m_head.getBlockCount(); i++)
				{
					iblock = new InfoBlock(dis);
					if (iblock.getBlockFingerprint() == InfoBlock.RLV_VIDEO_INFO)
					{
						m_info = new RLVInfo(dis);
					}
					else if(iblock.getBlockFingerprint() == InfoBlock.RLV_FRAME_DISTANCE_MAPPING &&
							iblock.getRecordCount()>0)
					{
						m_fdmapping = new RLVFrameDistanceMapping[iblock.getRecordCount()];
						for (int j=0; j<iblock.getRecordCount(); j++)
							m_fdmapping[j] = new RLVFrameDistanceMapping(dis);
					}
					else if(iblock.getBlockFingerprint() == InfoBlock.RLV_INFOBOX &&
							iblock.getRecordCount()>0)						
					{
						m_infobox = new RLVInfoBox[iblock.getRecordCount()];
						for (int j=0; j<iblock.getRecordCount(); j++)
							m_infobox[j] = new RLVInfoBox(dis);
					}
					else if(iblock.getBlockFingerprint() == InfoBlock.COURSE_INFO &&
							iblock.getRecordCount()>0)
					{
						m_courseinfo = new CourseInfo[iblock.getRecordCount()];
						for (int j=0; j<iblock.getRecordCount(); j++)
							m_courseinfo[j] = new CourseInfo(dis);
					}
				}
			}
		}
		catch (IOException e)
		{
			throw e;
		}				
	}
*/	
	public function RLVDefinition()
	{
		m_head = new Header(Header.RLV_FINGERPRINT, 100, 4)
	}
	
	public function getCourseinfo():Array
	{
		return m_courseinfo;
	}
	public function setCourseinfo(m_courseinfo:Array):void
	{
		this.m_courseinfo = m_courseinfo;
	}
	public function getFdmapping():Array
	{
		return m_fdmapping;
	}
	public function setFdmapping(m_fdmapping:Array):void
	{
		this.m_fdmapping = m_fdmapping;
	}
	public function getRLVinfo():RLVInfo
	{
		return m_info;
	}
	public function setRLVinfo(m_ginfo:RLVInfo):void
	{
		this.m_info = m_ginfo;
	}
	public function getHeader():Header
	{
		return m_head;
	}
	public function  setHeader(m_head:Header):void
	{
		this.m_head = m_head;
	}
	public function getInfobox():Array
	{
		return m_infobox;
	}
	public function  setInfobox(m_infobox:Array):void
	{
		this.m_infobox = m_infobox;
	}
	
	public function setInfoboxText(t:String):void
	{
		this.m_infoboxtext = t;
	}
	
	public function getInfoboxText():String
	{
		return this.m_infoboxtext;
	}
	
	public function toFile(file:File):void
	{
		var i:int = 0;
		var dos:ByteArray=new ByteArray();
		dos.endian = Endian.LITTLE_ENDIAN;
		// First the header is written
		m_head.persist(dos);				
		// Now create an infoblock for RLVinfo		
		var ib:InfoBlock = new InfoBlock(InfoBlock.RLV_VIDEO_INFO, 100, 1, RLVInfo.RECORD_SIZE);
		ib.persist(dos);
		m_info.persist(dos);
		// FD Mapping
		ib = new InfoBlock(InfoBlock.RLV_FRAME_DISTANCE_MAPPING, 100, m_fdmapping.length, RLVFrameDistanceMapping.RECORD_SIZE);
		ib.persist(dos);
		for (i=0; i<m_fdmapping.length; i++)
			m_fdmapping[i].persist(dos);
		// Infoboxes
		if (m_infobox == null)
		{
			ib = new InfoBlock(InfoBlock.RLV_INFOBOX, 100, 0, RLVInfoBox.RECORD_SIZE);
			ib.persist(dos);
		}
		else
		{
			ib = new InfoBlock(InfoBlock.RLV_INFOBOX, 100, m_infobox.length, RLVInfoBox.RECORD_SIZE);
			ib.persist(dos);
			for (i=0; i<m_infobox.length; i++)
				m_infobox[i].persist(dos);
		}
		// Course Info
		ib = new InfoBlock(InfoBlock.COURSE_INFO, 100, m_courseinfo.length, CourseInfo.RECORD_SIZE);		
		ib.persist(dos);
		for (i=0; i<m_courseinfo.length; i++)
			m_courseinfo[i].persist(dos);		
			
		var fs:FileStream = new FileStream();
		fs.open(file, FileMode.WRITE);
		fs.writeBytes(dos,0, dos.length);		
		fs.close();
	}
	
	public function infoboxToFile(file:File):void
	{
		var fs:FileStream = new FileStream();
		fs.open(file, FileMode.WRITE);
		fs.writeUTFBytes(this.m_infoboxtext);		
		fs.close();
	}
}

}